<template>
	<el-dialog :title="title" v-model="open" append-to-body :close-on-click-modal="false" width="300px">
		<el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
			<el-row :gutter="10" class="mb20">
				<el-col>
					<el-form-item label="充值金额" prop="money">
						<el-input v-model="form.money" placeholder="请输入充值金额" @input="verifyNumberIntegerAndFloatMoney" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col>
					<el-form-item label="操作密码" prop="password">
						<el-input v-model="form.password" :type="isShowPassword ? 'text' : 'password'" placeholder="请输入操作密码">
							<template #suffix>
								<i
									class="iconfont el-input__icon login-content-password"
									:class="isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
									@click="isShowPassword = !isShowPassword"
								>
								</i>
							</template>
						</el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button type="primary" :loading="submitLoading" @click="onSubmitForm"> 确 定 </el-button>
				<el-button @click="onCancel">取 消</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script lang="ts">
import { reactive, ref, toRefs } from 'vue';
import { userApi } from '/@/api/admin';
import { ElMessage } from 'element-plus';
import { verifyNumberIntegerAndFloat } from '/@/utils/toolsValidate';

const UserApi = userApi()

export default {
	setup() {
		const state = reactive({
			title: '余额充值',
			open: false,
			form: {} as any,
      rules: {
				money: [{ required: true, message: '充值金额不能为空', trigger: ['blur'] }],
      },
			submitLoading: false,
			isShowPassword: false,
		});

		const formRef = ref(null);
		let callback: Function = () => {};

		/********** 点击事件(on开头) **********/
		/** 提交按钮 */
		const onSubmitForm = () => {
			(formRef.value as any)?.validate((valid: boolean) => {
				if (valid) {
					state.submitLoading = true;
					_userRecharge(state.form);
				}
			});
		};
		// 取消按钮
		const onCancel = () => {
			state.submitLoading = false;
			state.open = false;
			reset();
		};

		/********** 方法事件 **********/
		//打开
		const openDialog = (config: any, fn: Function) => {
			console.log(config);
			reset();
			callback = fn;
			state.open = true;
			state.form.user_id = config.row.id
		};
		// 表单重置
		const reset = () => {
			state.form = {
				user_id: '',
				money: '',
				password:''
			};
			(formRef.value as any)?.resetFields();
		};
		// 充值金额
		const verifyNumberIntegerAndFloatMoney = (val: string) => {
			state.form.money = verifyNumberIntegerAndFloat(val);
		};

		/********** 网络事件 **********/
		// 充值金额
		const _userRecharge = (_data: object) => {
			UserApi.userRecharge(_data)
				.then(() => {
					ElMessage.success('操作成功');
					onCancel();
					callback();
				})
				.catch(() => (state.submitLoading = false));
		};

		return {
			formRef,
			onSubmitForm,
			openDialog,
			onCancel,
			verifyNumberIntegerAndFloatMoney,
			...toRefs(state),
		};
	},
};
</script>

<style lang="scss" scoped></style>
